# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html

import json
# from scrapy.exporters import CsvItemExporter

from openpyxl import Workbook


class BiSpiderPipeline(object):
    def __init__(self):
        self.f = open('tencent.json', 'wb+')

    def process_item(self, item, spider):
        content = json.dumps(dict(item), ensure_ascii=False) + ",\n"
        self.f.write(content.encode(encoding='UTF-8'))
        return item

    def close_spider(self, spider):
        self.f.close()


# 天猫
class TianmaoPipeline(object):
    # def __init__(self):
    #     self.f = open('tianmao.json', 'wb+')
    #
    # def process_item(self, item, spider):
    #     content = json.dumps(dict(item), ensure_ascii=False) + ",\n"
    #     self.f.write(content.encode(encoding='UTF-8'))
    #     return item
    #
    # def close_spider(self, spider):
    #     self.f.close()

    def open_spider(self, spider):
        # self.file = open("F:/python_envir/BI_Spider/tianmao.xls", "wb")
        # self.exporter = CsvItemExporter(self.file,
        #                                 fields_to_export=["goods_name", "jumpLink", "imageLink", "price", "shop_name",
        #                                                   "month_number", "comment_number"])
        # self.exporter.start_exporting()

        self.wb = Workbook()
        self.ws = self.wb.active
        self.ws.append(["goods_name", "jumpLink", "imageLink", "price", "shop_name", "month_number", "comment_number"])

    def process_item(self, item, spider):
        # self.exporter.export_item(item)
        line = [
            item['goods_name'],
            item['jumpLink'],
            item['imageLink'],
            item['price'],
            item['shop_name'],
            item['month_number'],
            item['comment_number']]
        self.ws.append(line)

        return item

    def close_spider(self, spider):
        # self.exporter.finish_exporting()
        # self.file.close()
        self.wb.save("F:/python_envir/BI_Spider/tianmao.xls")
        self.wb.close()
